import AI
import sys
# import time

from Chessnut import Game

chessgame = Game()

#def printreplace(boardlist):
    

def printboard(boardlist):
    out = ""
    n = 0
    for i in boardlist:
        if n % 8 == 0:
            out = out + "\n"
            out = out + str(n // 8 + 1) + " "
        if i != " ":
            out = out + i
        else:
            out = out + "."
        n += 1
    out = out + "\n"
    out = out + "  abcdefgh" + "\n"
    return out

print("This is Caibot V3.1")
while True:

    side = input("Which side you want play as?")
    if side == "w":
        print("OK.You can play as white.")
        print("And Caibot will play as black.")
        playnow = 0
        break
    elif side == "b":
        print("OK.You can play as black.")
        print("And Caibot will play as white.")
        playnow = 1
        break
    else:
        # print("InputError")
        # time.sleep(1)
        # print("exit")
        # time.sleep(2)
        # sys.exit(1)
        print("Couldn't parse input, enter a valid letter.")
        print("Input 'w' for white,input 'b' for black")
while True:
    deep = input("How deep do you want Caibot think?")
    try:
        deep = int(deep)
        if not deep < 0:
            break
        else:
            print("Couldn't parse input, enter a valid number.")
    except ValueError:
        print("Couldn't parse input, enter a valid number.")

lastscr = 0
while True:
    if playnow == 1:
        moves = chessgame.get_moves()
        if len(moves) == 0:
            print("you win.")
            break
        else:
            print("AI thinking...")
            move = AI.GiveBestMove(chessgame, side, deep, lastscr)
            chessgame.apply_move(move[0])
            print("move to: " + move[0])
            lasrscr = move[1]
            print(printboard(AI.boardlist(AI.StrGame(chessgame))))
            playnow = 0
    elif playnow == 0:
        moves = chessgame.get_moves()
        if len(moves) == 0:
            print("You lost.")
            break
        else:
            print("It's your turn.")
            while playnow == 0:
                move = input("move to: ")
                if move in moves:
                    chessgame.apply_move(move)
                    playnow = 1
                else:
                    print("Couldn't parse input, enter a valid move.")

input()
sys.exit(0)

